#ifndef __MUTEX_H__
#define __MUTEX_H__

#include <pthread.h>
#include <iostream>


class Mutex {
private:
  pthread_mutex_t m;
  pthread_cond_t cv;
public: 
  Mutex(const Mutex &c) {};

  Mutex() {pthread_cond_init(&cv, 0);pthread_mutex_init(&m, 0);}
  ~Mutex() {pthread_cond_destroy(&cv);pthread_mutex_destroy(&m);}
  void wait() {pthread_cond_wait(&cv, &m);}
  void signal() {pthread_cond_signal(&cv);}
  void  broad() {pthread_cond_broadcast(&cv);} 	
  void lock() {pthread_mutex_lock(&m);}
  void unlock() {pthread_mutex_unlock(&m);}
  class Lock;
};

class Lock {
private:
  Mutex &m;
  Lock(const Lock &c) : m(c.m) {};
public:
  explicit Lock(Mutex &m) : m(m) {m.lock();}
  ~Lock() {m.unlock();}
  void wait() {m.wait();}
  void signal() {m.signal();}
  void  broad() {m.broad();}
};

#endif
